//
//  VideoDownloadViewController.m
//  Rail
//
//  Created by hychen on 2018/3/8.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "VideoDownloadViewController.h"
#import "NSDate+Category_lib.h"
#import "HcdDateTimePickerView.h"
#import "NSDate+Category_lib.h"

@interface VideoDownloadViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UILabel *startLabel;
@property (nonatomic, strong) UILabel *endLabel;

@property (nonatomic, strong) UITextField *startField;
@property (nonatomic, strong) UITextField *endField;
@property (nonatomic, weak) UITextField *currentField;

@property (nonatomic, strong) HcdDateTimePickerView *startPickerView;
@property (nonatomic, strong) HcdDateTimePickerView *endPickerView;
@property (nonatomic, strong) UIButton *downloadButton;

@property (nonatomic, strong) NSDate *fromDate;
@property (nonatomic, strong) NSDate *toDate;
@end

@implementation VideoDownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self prepareData];
    
    [self prepareUI];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareData {
    NSDate *currentDate = [NSDate date];
    _toDate = [currentDate dateBySubtractingHours:1];
    _fromDate = [_toDate dateBySubtractingMinutes:1];
}

- (void)prepareUI {
    
    BMWeakSelf
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.tipLabel = [UILabel labelWithText:@"请选择要下载的时间段" textColor:Color_RGB(127, 127, 127) font:[UIFont systemFontOfSize:16]];
    self.tipLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.tipLabel];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.scrollView);
        make.height.mas_equalTo(45);
        make.width.mas_equalTo(weakSelf.view);
    }];
    
    self.startLabel = [UILabel labelWithText:@"起始时间:" textColor:Color_RGB(124, 223, 240) font:[UIFont systemFontOfSize:17]];
    self.startLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.startLabel];
    [self.startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView);
        make.top.equalTo(weakSelf.tipLabel.mas_bottom);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(50);
    }];
    
    self.endLabel = [UILabel labelWithText:@"结束时间:" textColor:Color_RGB(255, 50, 64) font:[UIFont systemFontOfSize:17]];
    self.endLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.endLabel];
    [self.endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView);
        make.top.equalTo(weakSelf.startLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(50);
    }];
    
    self.startField = [UITextField new];
    self.startField.font = [UIFont systemFontOfSize:18.f];
    self.startField.textColor = Color_RGB(124, 223, 240);
    self.startField.textAlignment = NSTextAlignmentCenter;
    self.startField.text = [_fromDate formatYearMonthDayHourSecond_lib];
    self.startField.delegate = self;
    [self.scrollView addSubview:self.startField];
    [self.startField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(180);
        make.left.equalTo(self.startLabel.mas_right);
        make.top.equalTo(self.startLabel);
        make.bottom.equalTo(self.startLabel);
    }];
    
    self.endField = [UITextField new];
    self.endField.font = [UIFont systemFontOfSize:18.f];
    self.endField.textColor = Color_RGB(255, 50, 64);
    self.endField.textAlignment = NSTextAlignmentCenter;
    self.endField.text = [_toDate formatYearMonthDayHourSecond_lib];
    self.endField.delegate = self;
    [self.scrollView addSubview:self.endField];
    [self.endField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(180);
        make.left.equalTo(self.endLabel.mas_right);
        make.top.equalTo(self.endLabel);
        make.bottom.equalTo(self.endLabel);
    }];
    
    self.downloadButton = [UIButton buttonWithTitle:@"下载" titleColor:[UIColor whiteColor] norImage:ImageNamed_bb(@"app_btn_n") highImage:ImageNamed_bb(@"app_btn_p") action:@selector(clickDownloadButton) owner:self];
    self.downloadButton.layer.cornerRadius = 22.5f;
    [self.scrollView addSubview:self.downloadButton];
    [self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.scrollView);
        make.left.mas_equalTo(25);
        make.top.mas_equalTo(weakSelf.endField.mas_bottom).offset(40);
        make.height.mas_equalTo(45);
    }];
}

- (void)clickDownloadButton {
    if (self.BlockClickDownload) {
        self.BlockClickDownload(_fromDate, _toDate);
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    self.currentField = textField;
    
    if (textField == self.startField) {
        [[UIApplication sharedApplication].keyWindow addSubview:self.startPickerView];
        self.startPickerView.maxDate = self.fromDate;
        [self.startPickerView showHcdDateTimePicker];
    }
    else if (textField == self.endField) {
        [[UIApplication sharedApplication].keyWindow addSubview:self.endPickerView];
        self.endPickerView.maxDate = [NSDate date];
        [self.endPickerView showHcdDateTimePicker];
    }

    return NO;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}
#pragma mark - getter

- (HcdDateTimePickerView *)startPickerView {
    if (!_startPickerView) {
        _startPickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerDateTimeMode defaultDateTime:_fromDate];
        _startPickerView.topViewColor = Color_RGB(247, 247, 247);
        _startPickerView.buttonTitleColor = Color_RGB(75, 200, 175);
        BMWeakSelf
        [_startPickerView setClickedOkBtn:^(NSString *dateTimeStr) {
            weakSelf.startField.text = dateTimeStr;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            //赋值日期
            weakSelf.fromDate = [fmt dateFromString:dateTimeStr];
        }];
        NSInteger currYear = _fromDate.year;
        [_startPickerView setMinYear:currYear-1];
        [_startPickerView setMaxYear:currYear];
    }
    return _startPickerView;
}

- (HcdDateTimePickerView *)endPickerView {
    if (!_endPickerView) {
        _endPickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerDateTimeMode defaultDateTime:_toDate];
        _endPickerView.topViewColor = Color_RGB(247, 247, 247);
        _endPickerView.buttonTitleColor = Color_RGB(75, 200, 175);
        BMWeakSelf
        [_endPickerView setClickedOkBtn:^(NSString *dateTimeStr) {
            weakSelf.endField.text = dateTimeStr;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            //赋值日期
            weakSelf.toDate = [fmt dateFromString:dateTimeStr];
            if (weakSelf.toDate == [weakSelf.toDate earlierDate:weakSelf.fromDate]) {
                weakSelf.fromDate = [weakSelf.toDate dateBySubtractingMinutes:5];
                weakSelf.startField.text = [weakSelf.fromDate formatYearMonthDayHourSecond_lib];
            }
        }];
        NSInteger currYear = _toDate.year;
        [_endPickerView setMinYear:currYear-1];
        [_endPickerView setMaxYear:currYear];
    }
    return _endPickerView;
}

@end
